
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .list {
            display: flex;
            text-align: center;
        }

        .disl {
            border: 1px solid #ddd;
            background-color: #eee;
            color: #aaa;
        }
    </style>
</head>

<body>
    <div class="list">

    </div>
    <button class="prev">上一页</button>
    <span></span>
    <button class="next">下一页</button>
    <script>
        var page = 1;//保存当前页码
        // 将对各市转为查询字符串格式
        function tostring(obj) {
            var str = '';
            for (var key in obj) {
                str += `${key}=${obj[key]}&`;
            }
            return str.slice(0, length - 1);
        }

        // 封装ajax请求
        function ajax(medthod, url, paparms) {
            return new Promise((resolve, reject) => {
                var xhr = new XMLHttpRequest();
                xhr.open(medthod, medthod == 'get' ? url + "?" + tostring(paparms) : url);
                xhr.onreadystatechange = function () {
                    if (xhr.readyState == 4) {
                        resolve(JSON.parse(xhr.responseText))
                    }
                }
                xhr.send();
            })
        }

        // 点击上一页，获取上一页的表情，渲染表情
        document.querySelector(".prev").onclick = function () {
            if (page > 1) {
                ajax('get', `https://yantianfeng.com/api/emotion`,
                    { page: --page, count: 10 }).then(function (num) {
                        dis()
                        var str = num.emotions.map((item, index) => {
                            return `<div><img src="${item.url}"/><div>${item.title}</div></div>`
                        }).join('')
                        document.querySelector('.list').innerHTML = str;
                    })
            }
        }
        // 点击下一页，获取下一页的表情，渲染表情
        document.querySelector(".next").onclick = function () {
            if (page < 4) {
                ajax('get', `https://yantianfeng.com/api/emotion`,
                    { page: ++page, count: 10 }).then(function (num) {
                        dis()
                        var str = num.emotions.map((item, index) => {
                            return `<div><img src="${item.url}"/><div>${item.title}</div></div>`
                        }).join('')
                        document.querySelector('.list').innerHTML = str;
                    })
            }
        }
        function dis() {
            var span = document.querySelector('span')
            span.innerHTML = page + '/' + 4
            switch (page) {
                case  1:
                    document.querySelector('.prev').classList.add("disl");
                    break;
                case  4:
                    document.querySelector('.next').classList.add("disl");
                    break;
                case 2:
                    document.querySelector('.next').classList.remove('disl')
                    document.querySelector('.prev').classList.remove('disl')
                    break;
                case 3:
                    document.querySelector('.next').classList.remove('disl')
                    document.querySelector('.prev').classList.remove('disl')
                    break;
                default:
                    break;
            }
        }
    </script>
</body>

</html>